package com.scy.weibotest01.controller;

import com.scy.weibotest01.mapper.WeiboMapper;
import com.scy.weibotest01.pojo.dto.WeiboDTO;
import com.scy.weibotest01.pojo.entity.Weibo;
import com.scy.weibotest01.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/weibo/")
public class WeiboController {
    @Autowired(required = false)
    WeiboMapper mapper;

    @RequestMapping("insert")
    public int insert(@RequestBody WeiboDTO weiboDTO, HttpSession session){
        UserVO user = (UserVO) session.getAttribute("user");
        if (user == null){
            return 2;
        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(user.getId());
        mapper.insert(weibo);
        return 1;
    }
}
